--
-- Author: mm
-- Date: 2019-03-09 11:16:54
--
-- NewpartnerView
--

local fileName = string.getModeName(...)
local PartnerView = class(fileName, app.mvc.ViewBase)
local PartnerInfoPanel = import(".PartnerInfoPanel")
local PartnerRelationPanel = import(".PartnerRelationPanel")

local newFloatTipsView = require("app.newFloatTips.newFloatTipsView")
local TabsBtn = require("app.widget.TabsBtn")

local PartnerHelper = require("app.partner.PartnerHelper")
PartnerView.VIEW_ROOT_RES = Res.PartnerView

function PartnerView:init()
    
    local btns = {}
    for i = 1,2 do
        btns[i] = self._ui["Panel/Panel_tab"]:getChildByName("selected_btn_"..i)
    end

    -- local infoPanel = PartnerInfoPanel.create(self._ui["Panel/Node_partner"])
    -- local relationPanel = PartnerRelationPanel.create(self._ui["Panel/fetter"])

    local tabsConfig = {
        {key = Enums.PartnerTab.Info, panel = nil},
        {key = Enums.PartnerTab.Relation, panel = nil}
    }
    self._tabsConfig = tabsConfig
    self._currentSelectTabIdx = 0
    self._tabsBtn = TabsBtn.new(btns, handler(self, self.onTabChangge))

    self._ui["Panel/Button_close"]:addClickEventListener(function()
		executeFunc(self._onCloseView)
    end)
    self._ui["Panel/Panel_parent"]:setVisible(true)
    self:registerViewEvent({
        -- {name = ViewEvent.Partner_EquipClick, callback = handler(self, self._onPartnerEquipClick)},
        {name = ViewEvent.Partner_OpenPartnerEquipStength, callback = handler(self, self._onOpenPartnerEquipStength)},
        {name = ViewEvent.Partner_SkillLevelUpClick, callback = handler(self, self._onPartnerSkillLevelUpClick)},
        {name = ViewEvent.Partner_OpenGetPop, callback = handler(self, self._onOpenPartnerGetPop)},
        {name = ViewEvent.Partner_ShowTips, callback = handler(self, self._onPartnerShowTips)},
        {name = ViewEvent.Partner_QualityLvUp, callback = handler(self, self._onOpenPartnerQualityLvUp)},
        {name = ViewEvent.Partner_ComposeBySoul, callback = handler(self, self._onComposePartnerBySoul)},
    })
end

function PartnerView:_getPanelByConfig(tabConfig)
    if not tabConfig.panel then
        local panel = nil
        if tabConfig.key == Enums.PartnerTab.Info then
            panel = PartnerInfoPanel.create()
        else
            panel = PartnerRelationPanel.create()
        end
        self._ui["Panel/Panel_parent"]:addChild(panel)
        tabConfig.panel  = panel
    end
    return tabConfig.panel
end

function PartnerView:onTabChangge(index, btn)
    if self._currentSelectTabIdx == index then
        return
    end
    self._currentSelectTabIdx = index
    local config = self._tabsConfig[index]
    if config then
        for k, v in pairs(self._tabsConfig) do
            if v.key == config.key then
                local panel = self:_getPanelByConfig(v)
                panel:setVisible(true)
            else
                if v.panel then
                    v.panel:setVisible(false)
                end
            end
        end
    end
end

function PartnerView:_onPartnerSkillLevelUpClick(eventName, render)
    if not render then
        return
    end
    local data = render:getData()
    if not data then
        return
    end
    executeFunc(self._onOperate, "openPartnerSkillUpPop", data)
end

function PartnerView:_onOpenPartnerEquipStength(eventName, render)
    if not render then
        return
    end
    local data = render:getData()
    if not data then
        return
    end
    self:hideTips()
    executeFunc(self._onOperate, "openPartnerEquipView", data)
end

function PartnerView:_onOpenPartnerQualityLvUp(eventName, render)
    if not render then
        return
    end
    local data = render:getData()
    if not data then
        return
    end
    self:hideTips()
    executeFunc(self._onOperate, "openPartnerQualityLvUp", data)
end


function PartnerView:_onPartnerShowTips(eventName, params, otherParams)
    self:showTips(params, otherParams)
end

function PartnerView:_onOpenPartnerGetPop(eventName, data)
    executeFunc(self._onOperate, "openPartnerGetPop", data)
end

function PartnerView:_onComposePartnerBySoul(eventName, data)
    executeFunc(self._onOperate, "startUnlockPartner", data)
end

function PartnerView:hideTips()
    if self._isTipShow then
        self._isTipShow = false
        if not cc.isDead(self._tipView) then
            self._tipView:setVisible(false) 
            self._clipNode:setVisible(false)
        end
    end
end

function PartnerView:showTips(tipsParams, otherParams)
    if not self._tipView then
        self:initTipsView()
    end

    tipsParams.closeCallBack = function()
        self:hideTips()
    end

    self._tipView:refreshContent(tipsParams)
    self._tipView:setVisible(true) 
    self._clipNode:setVisible(true)

    self._isTipShow = true
    if otherParams.tipPos then
        local size = self._tipView.mUINode:getContentSize()
        local targetX = otherParams.tipPos.x - size.width -10
        local targetY = otherParams.tipPos.y - size.height

        if targetX < 0 then
            targetX = 0
        end
        if targetY < 20 then
            targetY = 20
        end
        self._tipView:setPosition(cc.p(targetX, targetY))
    end

    if otherParams.clipPos then
        self._stencilSprite:setPosition(otherParams.clipPos)
    end

    if otherParams.clipSize then
        self._stencilSprite:setContentSize(otherParams.clipSize)
    else
        self._stencilSprite:setContentSize(cc.size(0, 0))
    end
end

function PartnerView:initTipsView()
    -- local floatTipBg = display.newColorLayer(cc.c4b(0,0,0,255*0.45))
    local floatTipBg = ccui.Layout:create()
    floatTipBg:setContentSize(display.sizeInPixels)
    floatTipBg:setBackGroundColorType(1)
    floatTipBg:setBackGroundColor(cc.c3b(0,0,0))
    floatTipBg:setOpacity(255 * 0.4)
    floatTipBg:setTouchEnabled(true)
    floatTipBg:setSwallowTouches(false)
    -- floatTipBg:addClickEventListener(function()
    -- end)
    local clipNode = cc.ClippingNode:create()
    self._stencilSprite = display.newScale9Sprite("public/public_black.png",0, 0, cc.size(100, 100))
    self._stencilSprite:setAnchorPoint(0, 0)
    self._clipNode = clipNode
    clipNode:addChild(floatTipBg)
    clipNode:setStencil(self._stencilSprite)
    clipNode:setAlphaThreshold(0)
    clipNode:setInverted(true)
    self:addChild(clipNode,199)

    self._tipView = newFloatTipsView.new( {} )
    self:addChild(self._tipView, 200)
end



return PartnerView
